package org.jeecg.modules.kuner.usertakecollectionbase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;

import org.jeecg.modules.kuner.bankcardbase.entity.KrBankcardBase;
import org.jeecg.modules.kuner.bankcardbase.service.IKrBankcardBaseService;
import org.jeecg.modules.kuner.userpointstatement.service.ISdUserPointStatementService;
import org.jeecg.modules.kuner.userpointstatement.vo.AskTakeIncomeVO;
import org.jeecg.modules.kuner.usertakecollectionbase.entity.SdUserTakeCollectionBase;
import org.jeecg.modules.kuner.usertakecollectionbase.mapper.SdUserTakeCollectionBaseMapper;
import org.jeecg.modules.kuner.usertakecollectionbase.service.ISdUserTakeCollectionBaseService;
import org.jeecg.modules.kuner.usertakecollectionbase.vo.JudgeTakeCollectionVO;
import org.jeecg.modules.putil.KrStatic;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @Description: 提现审核记录
 * @Author: jeecg-boot
 * @Date: 2024-10-28
 * @Version: V1.0
 */
@Slf4j
@Service
public class SdUserTakeCollectionBaseServiceImpl extends ServiceImpl<SdUserTakeCollectionBaseMapper, SdUserTakeCollectionBase> implements ISdUserTakeCollectionBaseService {

    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISdUserPointStatementService userPointStatementService;
    @Resource
    private IKrBankcardBaseService bankcardBaseService;

    /**
     * 根据用户信息获取总收益
     *
     * @param userId 用户信息
     * @return 查询出来的总收益
     */
    @Override
    public BigDecimal getIncomeCountByUserId(String userId) {
        return getIncomeByUserId(userId).stream().filter(income -> KrStatic.TakeCollectionStatus.FINISH.name().equals(income.getJudgeStatus())).map(SdUserTakeCollectionBase::getTakeAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    /**
     * 根据用户信息查询用户收益记录
     *
     * @param userId 用户信息
     * @return 查询到的用户收益记录
     */
    public List<SdUserTakeCollectionBase> getIncomeByUserId(String userId) {
        return list(new LambdaQueryWrapper<SdUserTakeCollectionBase>().eq(SdUserTakeCollectionBase::getUserId, userId));
    }

    /**
     * 申请提现
     *
     * @param askTakeIncomeVO 申请提现必要的参数信息
     */
    @Override
    public void askTakeIncome(AskTakeIncomeVO askTakeIncomeVO) {
        String userId = askTakeIncomeVO.getUserId();
        SysUser sysUser = sysUserService.getOptById(userId).orElseThrow(() -> new JeecgBootException("无法找到对应的用户信息！"));
        BigDecimal realTakeAmount;

        // 可提现的积分数值
        BigDecimal takeableAmount = userPointStatementService.getTakeablePointCountByUserId(userId);
        // 要提现的马内数值
        BigDecimal takeAmount = askTakeIncomeVO.getTakeAmount();
        if (takeAmount != null) {
            realTakeAmount = takeAmount.setScale(2, RoundingMode.DOWN);
        } else {
            realTakeAmount = takeableAmount;
        }
        if (realTakeAmount.compareTo(takeableAmount) > 0) {
            throw new JeecgBootException("提现金额不得大于可提现金额！");
        }
        if (realTakeAmount.compareTo(BigDecimal.TEN) < 0) {
            throw new JeecgBootException("最小提现金额为10！");
        }
        if (realTakeAmount.stripTrailingZeros().scale() > 0) {
            throw new JeecgBootException("金额非整数无法提现！");
        }
        KrBankcardBase bankcardBase = bankcardBaseService.getOptById(askTakeIncomeVO.getBankId()).orElseThrow(() -> new JeecgBootException("找不到对应的提现信息！"));
        // 存入提现集
        SdUserTakeCollectionBase userTakeCollectionBase = new SdUserTakeCollectionBase();
        userTakeCollectionBase.setTakeAmount(realTakeAmount);
        userTakeCollectionBase.setUserId(sysUser.getId());
        userTakeCollectionBase.setUserName(sysUser.getRealname());
        userTakeCollectionBase.setJudgeStatus(KrStatic.TakeCollectionStatus.AWAIT.name());
        userTakeCollectionBase.setTakeImage(bankcardBase.getBankInfo());
        userTakeCollectionBase.setTakeAccount(bankcardBase.getCardNo());
        save(userTakeCollectionBase);
        // 变更用户积分
        userPointStatementService.userModifyPoint(realTakeAmount.negate(), CommonConstant.POINT_MODIFY_MSG_TOOK, userId);
    }


    /**
     * 审核提现集
     *
     * @param judgeTakeCollectionVO 审核体现及必要的参数信息
     */
    @Override
    public void judgeTakeCollection(JudgeTakeCollectionVO judgeTakeCollectionVO) {
        SdUserTakeCollectionBase takeCollectionBase = getOptById(judgeTakeCollectionVO.getTakeCollectionId()).orElseThrow(() -> new JeecgBootException("无法找到审核的提现集信息！"));
        if (!KrStatic.TakeCollectionStatus.AWAIT.name().equals(takeCollectionBase.getJudgeStatus()))
            throw new JeecgBootException("该提现集已经被审核！");
        takeCollectionBase.setJudgeMsg(judgeTakeCollectionVO.getJudgeMsg());
        takeCollectionBase.setJudgeStatus(judgeTakeCollectionVO.getJudgeStatus().name());
        updateById(takeCollectionBase);
        if (KrStatic.TakeCollectionStatus.OBJECTION.equals(judgeTakeCollectionVO.getJudgeStatus())) {
            log.info("提现审核驳回，进行收益归还！");
            userPointStatementService.userModifyPoint(takeCollectionBase.getTakeAmount(), CommonConstant.POINT_MODIFY_MSG_TOOK_OBJECTION, takeCollectionBase.getUserId());
        }
    }
}
